// nursehome.js
var api = require('../../../config/api.js');
const util = require('../../../utils/util.js');
var check = require('../../../utils/check.js');

const app = getApp();

var permitChannel = [
  {
    id: 0,
    type: 'page',
    enable: true,
    iconUrl: "/static/patient/images/my_nurse.png",
    name: "我的护士",
    url: "/pages/patient/nurseinfo/nurseinfo"
  },
  {
    id: 1,
    type: 'page',
    enable: true,
    iconUrl: "/static/patient/images/my_mantain.png",
    name: "我的维护",
    url: "/pages/patient/condition-records/records?patientId=" + app.globalData.patientId
  },
  {
    id: 2,
    type: 'page',
    enable: true,
    iconUrl: "/static/patient/images/my_observe.png",
    name: "我的观察",
    url: "/pages/patient/condition/condition"
  },
  {
    id: 3,
    type: 'page',
    enable: true,
    iconUrl: "/static/patient/images/my_query.png",
    name: "咨询记录",
    url: "/pages/common/chat-window/chat-window?local=" + app.globalData.metaId + "&peer=" + app.globalData.myNurseId
  },
  {
    id: 4,
    type: 'page',
    enable: true,
    iconUrl: "/static/patient/images/my_info.png",
    name: "我的资料",
    url: '/pages/patient/patientinfo/patientinfo'
  }
];

var noPermitChannel = [
  {
    id: 0,
    type: 'bindtap',
    enable: true,
    iconUrl: "/static/nurse/images/my-patients.png",
    name: "我的病人",
    url: "/pages/nurse/mypatient/mypatient"
  },
  {
    id: 1,
    type: 'bindtap',
    enable: true,
    iconUrl: "/static/nurse/images/message.png",
    name: "查看留言",
    url:"/pages/nurse/chatlist/chatlist"
  },
  {
    id: 2,
    type: 'bindtap',
    enable: true,
    iconUrl: "/static/nurse/images/publishvideo.png",
    name: "发布视频",
    url: "/pages/nurse/publishvideo/publishvideo"
  },
  {
    id: 4,
    type: 'bindtap',
    enable: true,
    iconUrl: "/static/nurse/images/qualification.png",
    name: "维护资质",
    url: "/pages/nurse/qualifications/qualifications"
  },
  {
    id: 6,
    type: 'bindtap',
    enable: true,
    iconUrl: "/static/nurse/images/modify_info.png",
    name: "修改资料",
    url: "/pages/nurse/modify-info/modify-info"
  }
];

var permitChannel = [
    {
      id: 0,
      type: 'page',
      enable: true,
      iconUrl: "/static/nurse/images/my-patients.png",
      name: "我的病人",
      url: "/pages/nurse/mypatient/mypatient"
    },
    {
      id: 1,
      type: 'page',
      enable: true,
      iconUrl: "/static/nurse/images/message.png",
      name: "查看留言",
      url:"/pages/nurse/chatlist/chatlist"
    },
    {
      id: 2,
      type: 'page',
      enable: true,
      iconUrl: "/static/nurse/images/publishvideo.png",
      name: "发布视频",
      url: "/pages/nurse/publishvideo/publishvideo"
    },
    {
      id: 4,
      type: 'page',
      enable: true,
      iconUrl: "/static/nurse/images/qualification.png",
      name: "维护资质",
      url: "/pages/nurse/qualifications/qualifications"
    },
    {
      id: 6,
      type: 'page',
      enable: true,
      iconUrl: "/static/nurse/images/modify_info.png",
      name: "修改资料",
      url: "/pages/nurse/modify-info/modify-info"
    }
];

Page({

    /**
     * 页面的初始数据
     */
    data: {
      userInfo: {},
    },
  
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
      console.log("nursehome page onload get data");

      this.getNurseInfo();
      this.getIndexData();
    },
  
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },
  
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
      //util.getVerifyStatusfromServer().then(function(res){
      //  console.log("getVerifyStatusfromServer" + res);
      //});
    },
  
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {
  
    },
  
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
  
    },
  
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
  
    },
  
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
  
    },
  
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
  
    },
  getIndexData: function () {
    let that = this;
    check.checkPermit().then(function(res){
      if(res){
        that.setData({
          channel: permitChannel
        });
      }else{
        that.setData({
          channel: noPermitChannel
        });
      }
    });
  },

    getNurseInfo: function () {
      let that = this;
      let userInfoTemp = wx.getStorageSync('userInfo');
      console.log("getNurseInfo with url: " + api.NurseInfoList + ' app.globalData.metaId ' + app.globalData.metaId);
      util.request(api.NurseInfoList,{
        id: app.globalData.metaId
      }).then(function(res){
        console.log("getNurseInfo" + JSON.stringify(res));
        var info_temp = res['data'];
        if(info_temp!=null && info_temp!=""){
          console.log("getNurseInfo info_temp " + info_temp['name']);
          that.setData({
            userInfo: {
              id: info_temp['jobNumber'],
              name: info_temp['name'],
              headPortrait: app.globalData.avatarUrl,
              department: info_temp['department'],
            }
          });
        }
      });
    },
    gotoMyPatients: function () {
      wx.navigateTo({
        url: "/pages/nurse/mypatient/mypatient"
      });
    },

    btClick: function(event){
      let that = this;
      let id = event.currentTarget.dataset.id;
      console.log("btClick: " + id);
      check.checkPermit().then(function(res){
        if(res){
          that.setData({
            channel: permitChannel
          });
        }else{
          wx.showModal({
            title: '未审核，请耐心等待...'
          });
        }
      });
    },
  })